<?php

namespace app\index\service;

use app\common\model\Trade;
use app\index\dao\TradeDao;
use app\index\model\UM as UM;
use ShidianUtil\Exception\DataNotExistException;
use think\exception\ValidateException;

/**
 * Class OrderRefundClickService
 * @package app\index\service
 */
class OrderRefundClickService
{
    // dao
    private TradeDao $tradeDao;

    public function __construct(TradeDao $tradeDao)
    {
        $this->tradeDao = $tradeDao;
    }

    /**
     * 更新
     * @param UM $user
     * @param int $id
     * @param array $params
     * @return void
     */
    public function modify(UM $user, int $id, array $params): void
    {
        //这里如果要修改查询，建议把查询就放到这里
        $record = Trade::field(true)->where('id', $id)->find();

        if (is_null($record)) {
            throw new DataNotExistException('该订单不存在或已被删除');
        }

        if ($user->id != $record->memberId) {
            throw new ValidateException('您不能点击该订单');
        }

        if ($record->refundRequestFirstClickTime != 0) {
            return;
        }

        $record->refundRequestFirstClickTime = time();
        $record->save();
    }

}
